<template>
  <div class="app-container">
    <div class="filter-container">
      <el-select
        v-if="query.recruitmentId == null"
        v-model="listQuery.recruitmentId"
        class="filter-item"
        style="width: 200px;margin-right: 10px"
        placeholder="请选择职位"
      >
        <el-option
          v-for="item in recruitmentOptions"
          :key="item.recruitmentId"
          :label="item.recruitmentName"
          :value="item.recruitmentId"
        />
      </el-select>
      <el-date-picker
        v-model="deliveryTime"
        type="daterange"
        align="right"
        unlink-panels
        range-separator="至"
        start-placeholder="投递日期下限"
        end-placeholder="投递日期上限"
        :picker-options="pickerOptions"
        value-format="yyyy-MM-dd HH:mm:ss"
        style="margin-right: 10px"
      />
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        size="medium"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        size="medium"
        type="primary"
        icon="el-icon-refresh"
        @click="reset"
      >
        重置
      </el-button>
      <el-button
        class="filter-item"
        style="margin-left: 10px;"
        size="medium"
        type="success"
        icon="el-icon-s-promotion"
        @click="handleInterviewList"
      >
        邀约面试
      </el-button>
    </div>

    <div class="table-item">
      <el-table
        :key="tableKey"
        :data="list"
        border
        fit
        highlight-current-row
        style="width: 100%;"
        @selection-change="handleSelectionChange"
      >
        <el-table-column
          type="selection"
          width="55"
        />
        <el-table-column label="序号" width="50" align="center">
          <template slot-scope="scope">
            {{ listQuery.limit * (listQuery.page - 1) + (scope.$index + 1) }}
          </template>
        </el-table-column>
        <el-table-column label="姓名" align="center">
          <template slot-scope="{row}">
            <span>{{ row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="身份" align="center">
          <template slot-scope="{row}">
            <span>{{ row.resIdentity | dataFilter(resIdentityOperations) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" width="100" align="center">
          <template slot-scope="{row}">
            <span>{{ row.resState | dataFilter(resStateOperations) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="期望薪资" align="center">
          <template slot-scope="{row}">
            <span>{{ row.expectSalary }}k</span>
          </template>
        </el-table-column>
        <el-table-column label="投递职位" align="center">
          <template slot-scope="{row}">
            <span>{{ row.recruitmentName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="投递时间" width="200%" align="center">
          <template slot-scope="{row}">
            <span>{{ $moment(row.deliveryTime).format('YYYY-MM-DD HH:MM:SS') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="400%" class-name="small-padding fixed-width">
          <template slot-scope="{row}">
            <el-button size="mini" type="primary" icon="el-icon-view" plain @click="handleView(row.resumeId)">
              查看
            </el-button>
            <el-button
              v-if="query.status !== 1"
              size="mini"
              type="success"
              icon="el-icon-circle-check"
              plain
              @click="handleYes(row.id)"
            >
              有意向
            </el-button>
            <el-button
              v-if="query.status !== 2"
              size="mini"
              type="warning"
              icon="el-icon-circle-close"
              plain
              @click="handleNo(row.id)"
            >
              无意向
            </el-button>
            <el-popconfirm
              v-if="query.status !== 1"
              title="是否屏蔽该用户？"
              @onConfirm="handleBlack(row.resumeId)"
            >
              <el-button slot="reference" class="ml10" type="info" icon="el-icon-warning-outline" plain size="mini">屏蔽
              </el-button>
            </el-popconfirm>
            <el-button
              v-if="query.status === 1"
              size="mini"
              type="success"
              icon="el-icon-s-promotion"
              plain
              @click="handleInterview(row.id)"
            >
              邀约面试
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.limit"
        @pagination="getList"
      />
    </div>

    <!--    邀约面试-->
    <el-dialog title="邀约面试" :visible.sync="dialogFormVisible" width="30%">
      <el-form ref="ruleForm" :model="interviewForm" :rules="rules">
        <el-form-item label="面试地点" prop="interviewAddress">
          <el-input v-model="interviewForm.interviewAddress" style="width: 80%;" placeholder="输入面试地点" />
        </el-form-item>
        <el-form-item label="面试时间" prop="interviewTime">
          <el-date-picker
            v-model="interviewForm.interviewTime"
            type="datetime"
            style="width: 80%;"
            value-format="yyyy-MM-dd HH:mm:ss"
            placeholder="选择日期时间"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancelInterview">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getRecruitmentOptions
} from '@/api/recruitment'
import {
  fetchResumeListByRequest,
  acceptResume,
  rejectResume,
  employerViewResume
} from '@/api/resume'
import {
  pullBlack
} from '@/api/user'
import {
  sendInterview
} from '@/api/interview'
import { resIdentityOperations, resStateOperations } from '../../../utils/constants'
import Pagination from '@/components/Pagination/index'

export default {
  name: 'GenericResume',
  components: { Pagination },
  props: {
    query: {
      type: Object,
      default: () => {
        return {
          recruitmentId: null,
          status: 0
        }
      }
    }
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listQuery: {
        page: 1,
        limit: 10,
        status: this.query.status,
        recruitmentId: this.query.recruitmentId,
        deliveryTimeFrom: undefined,
        deliveryTimeTo: undefined
      },
      deliveryTime: undefined,
      recruitmentOptions: undefined,
      resIdentityOperations: resIdentityOperations,
      resStateOperations: resStateOperations,
      interviewForm: {
        deliveryId: undefined,
        interviewAddress: undefined,
        interviewTime: undefined
      },
      dialogFormVisible: false,
      multipleSelection: undefined,
      rules: {
        interviewAddress: [
          { required: true, message: '请输入面试地点', trigger: 'blur' }
        ],
        interviewTime: [
          { required: true, message: '请选择面试时间', trigger: 'blur' }
        ]
      },
      pickerOptions: {
        shortcuts: [
          {
            text: '最近三天',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 3)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近一周',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 7)
              picker.$emit('pick', [start, end])
            }
          },
          {
            text: '最近一个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
              picker.$emit('pick', [start, end])
            }
          }, {
            text: '最近三个月',
            onClick(picker) {
              const end = new Date()
              const start = new Date()
              start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
              picker.$emit('pick', [start, end])
            }
          }]
      }
    }
  },
  created() {
    this.getRecruitmentOptions()
    this.getList()
  },
  methods: {
    // 获取该求职者发布的职位列表
    getRecruitmentOptions() {
      getRecruitmentOptions().then(
        (response) => {
          this.recruitmentOptions = response.data
        },
        (error) => {
          this.$message.error(error.message)
        }
      )
    },
    // 获取未处理简历列表
    getList() {
      if (this.deliveryTime != null) {
        this.listQuery.deliveryTimeFrom = this.deliveryTime[0]
        this.listQuery.deliveryTimeTo = this.deliveryTime[1]
      }
      fetchResumeListByRequest(this.listQuery).then(
        (response) => {
          this.list = response.data.deliveryResumeList
          this.total = response.data.count
        },
        (error) => {
          this.$message.error(error.message)
        }
      )
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    // 重置
    reset() {
      this.listQuery = {
        page: 1,
        limit: 10,
        status: this.query.status,
        recruitmentId: undefined,
        deliveryTimeFrom: undefined,
        deliveryTimeTo: undefined
      }
      this.deliveryTime = undefined
      this.getList()
    },
    // 查看简历
    handleView(id) {
      employerViewResume({ resumeId: id }).then(
        (response) => {
          this.$router.push({ path: '/resume/view', query: { id: id }})
        },
        (error) => {
          this.$message.error(error.message)
        }
      )
    },
    handleYes(id) {
      const param = { deliveryId: id }
      acceptResume(param).then(
        (response) => {
          this.$message.success('已成功设置为有意向')
          this.getList()
        },
        (error) => {
          this.$message.error(error.message)
        }
      )
    },
    handleNo(id) {
      const param = { deliveryId: id }
      rejectResume(param).then(
        (response) => {
          this.$message.warning('已成功设置为无意向')
          this.getList()
        },
        (error) => {
          this.$message.error(error.message)
        }
      )
    },
    handleBlack(id) {
      const param = { resumeId: id }
      pullBlack(param).then(
        (response) => {
          this.$message.info('已成功将该求职者屏蔽')
          this.getList()
        },
        (error) => {
          this.$message.error(error.message)
        }
      )
    },
    handleSelectionChange(val) {
      this.multipleSelection = val
    },
    handleInterview(id) {
      this.interviewForm.deliveryId = id
      this.dialogFormVisible = true
    },
    handleInterviewList() {
      // 首先进行校验
      if (this.multipleSelection == null) {
        this.$message.info('请至少选择一份有意向简历')
        return
      }
      // 处理数据
      let deliveryIdList = ''
      for (let j = 0; j < this.multipleSelection.length; j++) {
        deliveryIdList = deliveryIdList + '-' + parseInt(this.multipleSelection[j].id)
      }
      this.interviewForm.deliveryId = deliveryIdList
      this.dialogFormVisible = true
    },
    // 邀约面试
    submitForm() {
      // console.log(this.interviewForm)
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          sendInterview(this.interviewForm).then(
            (response) => {
              this.dialogFormVisible = false
              this.$message.success('面试邀约发送成功')
              this.getList()
            },
            (error) => {
              this.$message.error(error.message)
            }
          )
        } else {
          return false
        }
      })
    },
    // 取消邀约
    cancelInterview() {
      this.dialogFormVisible = false
      this.$nextTick(() => {
        this.$refs['ruleForm'].clearValidate()
      })
    }
  }
}
</script>
<style lang="scss" scoped>
  .app-container {
    min-height: 500px;
  }

  .table-item {
    margin-top: 20px;
  }

  .mr10 {
    margin-right: 10px;
  }

  .ml10 {
    margin-left: 10px;
  }

  .filter-container {
    /*padding-top: 10px;*/
    padding-bottom: 10px;
  }
</style>
